var merchantDao = require('../../dao/merchantDao');
const winston = require('../../../app/utils/winstonLogger.js');
const _ = require('lodash');

module.exports = {
    findMerchantsByStatus : findMerchantsByStatus,
    findAllMerchants : findAllMerchants,
    updateMerchantStatus : updateMerchantStatus,
    findMerchantById : findMerchantById
}

function findMerchantsByStatus(status, callback){
    let merchantStatus = saveTrans(status);
    merchantDao.findMerchant({status: merchantStatus}, (err, result)=>{
        if(err){
            winston.error("findMerchantsByStatus service error!");
            callback(err,null);
        }else{
            result.forEach(function(value, index) {
                result[index].status = returnTrans(result[index].status);
                result[index].password = "";
            }, this);
            callback(null, result);
        }
    });
}

function findAllMerchants(callback){
    merchantDao.findMerchant({},(err, result)=>{
        if(err){
            winston.error("findAllMerchants service error!");
            callback(err,null);
        }else{
            result.forEach(function(value, index) {
                result[index].status = returnTrans(result[index].status);
                result[index].password = "";
            }, this);
            callback(null, result);
        }
    });
}

function findMerchantById(merchantId, callback){
    merchantDao.findMerchant({_id : merchantId},(err,result)=>{
        if(err){
            winston.error("findMerchantById service error!");
            callback(err, null);
        }else{
            result.forEach(function(value, index) {
                result[index].status = returnTrans(result[index].status);
                result[index].password = "";
            }, this);
            callback(null, result);
        }
    });
}

function updateMerchantStatus(merchant, callback){
    merchant.status = saveTrans(merchant.status);
    merchantDao.updateMerchantStatus(merchant, (err, result)=>{
        if(err){
            winston.error("updateMerchantStatus service error!");
            callback(err, null);
        }else{
            callback(null, result);
        }
    });
}

//result trans
function saveTrans(num){
    let merchantStatus = "";
    if(num == 0){
        merchantStatus = "未激活";
    }else if(num == 1){
        merchantStatus = "正常";
    }else if(num == 2){
        merchantStatus = "拉黑";
    }else if(num == 3){
        merchantStatus = "驳回";
    }
    return merchantStatus;
}

//result trans
function returnTrans(status){
    let num = 0;
    if(status == "未激活"){

    }else if(status == "正常"){
        num = 1;
    }else if(status == "拉黑"){
        num = 2;
    }else if(status == "驳回"){
        num = 3;
    }
    return num;
}
